/**
 * Created by Administrator on 2015/12/22.
 */
var Promise = require('promise');
var logger = require('log4js').getLogger('worksService');
var worksDao = require('../dao/WorksDao');
var collectDao = require('../dao/CollectDao');
var likeDao = require('../dao/LikeDao');
var commentDao = require('../dao/CommentDao');
var worksService = {};

worksService.checkJoin = function(memberId,activityId){
    var params = {memberId:memberId,activityId:activityId};
    return worksDao.getByParams(params,'id');
}
worksService.queryPage = function(activityId,pageNo,pageSize,type){
    var params = {activityId:activityId};
    params.bak2 = 0;
    var orderBy = '';
    if(type==1){
        orderBy = 'create_time desc';
    }else if(type==2){
        orderBy = 'bak1 desc';
    }else{
        orderBy = 'id';
    }
    return worksDao.getByPage(params,pageNo,pageSize,orderBy)
}

worksService.queryWorkPage = function(activityId,pageNo,pageSize,type){
    var params = {activityId:activityId};
    params.bak2 = 0;
    var orderBy = '';
    if(type==1){
        orderBy = 'create_time desc';
    }else if(type==2){
        orderBy = 'bak1 desc';
    }else{
        orderBy = 'id';
    }
    return worksDao.queryWorkPage(params,pageNo,pageSize,orderBy)
}

worksService.getById = function(workId){
    return worksDao.getById(workId);
}

worksService.checkCollect = function(workId,memberId){
    var params = {};
    params.articleId = workId;
    params.memberId = memberId;
    params.status = 1;
    return collectDao.getByParams(params,'id');
}

worksService.checkLike = function(workId,memberId){
    var params = {};
    params.articleId = workId;
    params.memberId = memberId;
    params.status = 1;
    return likeDao.getByParams(params,'id');
}

worksService.comment = function(workId,memberId,content){
    var comment = {};
    comment.articleId = workId;
    comment.memberId = memberId;
    comment.content = content;
    comment.createTime = new Date();
    comment.bak1 = 1;
    comment.bak2 = 0;
    return commentDao.save(comment);
}

worksService.collect = function(workId,memberId){
    var collect = {};
    collect.articleId = workId;
    collect.memberId = memberId;
    collect.status = 1;
    return collectDao.getByParams(collect,'id').then(function (c) {
        if(c&& c.length>0){
            return Promise.resolve(99);
        }else{
            collect.createTime = new Date();
            return collectDao.save(collect);
        }
    })

}

worksService.unCollect = function (workId,memberId) {
    var collect = {};
    collect.articleId = workId;
    collect.memberId = memberId;
    collect.status = 1;
    return collectDao.getByParams(collect,'id').then(function (c) {
        if(c&& c.length>0){
            var id = c[0].id;
            return collectDao.delete(id);
        }else{
            return Promise.resolve(collect);
        }
    })
}
worksService.like = function(workId,memberId){
    var like = {};
    like.articleId = workId;
    like.memberId = memberId;
    like.status = 1;
    return likeDao.getByParams(like,'id').then(function (l) {
        if(l&& l.length>0){
            return Promise.resolve(99);
        }else{
            like.createTime = new Date();
            return likeDao.save(like);
        }
    })

}

worksService.unLike = function(workId,memberId){
    var like = {};
    like.articleId = workId;
    like.memberId = memberId;
    like.status = 1;
    return likeDao.getByParams(like,'id').then(function (l) {
        if(l&& l.length>0){
            var id = l[0].id;
            return likeDao.delete(id);
        }else{
            return Promise.resolve(like);
        }
    })

}

worksService.save = function(work){
    return worksDao.save(work);
}

worksService.getJoinWork = function (memberId,activityId) {
    var params = {};
    params.memberId = memberId;
    params.activityId = activityId;
    return worksDao.getByParams(params,'id').then(function (ws) {
        if(ws&&ws.length>0){
            return Promise.resolve(ws[0]);
        }else{
            return Promise.resolve(null);
        }
    })
}
worksService.update = function (w) {
    return worksDao.update(w);
}
module.exports = worksService;